package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description:
 * @author: JC
 * @date: 2021/10/25 15:39
 */

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;
    /**
     * 使用异步编排
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        HashMap<String, Object> map = new HashMap<>();

        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            // 获取到的数据是skuInfo + skuImagesList
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        CompletableFuture<Void> categoryViewFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            Long category3Id = skuInfo.getCategory3Id();
            // 获取分类数据
            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            map.put("categoryView",categoryView);
        },threadPoolExecutor);


        CompletableFuture<Void> spuSaleAttrFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // 获取销售属性 + 销售属性值
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        },threadPoolExecutor);


        CompletableFuture<Void> skuValueIdsMapFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // 查询销售属性Id 与skuId组合的map
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String skuValueMapJSON = JSON.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson",skuValueMapJSON);
        },threadPoolExecutor);

        // 单独获取价格
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        },threadPoolExecutor);

        CompletableFuture<Void> incrHotScoreFuture = CompletableFuture.runAsync(() -> {
            // 调用热度排名方法
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);



        // 使用多任务进行组合
        CompletableFuture.allOf(
                skuInfoFuture,
                categoryViewFuture,
                spuSaleAttrFuture,
                skuValueIdsMapFuture,
                priceFuture,
                incrHotScoreFuture
                ).join();

        // 返回map 集合
        return map;
    }
}
